#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
struct ListNode 
{
    int val;
    struct ListNode* next;
};

typedef struct ListNode Node;
struct ListNode* removeElements(struct ListNode* head, int val)
{
    Node* prev = NULL;
    Node* cur = head;
    while (cur)
    {
        if (cur->val == val)
        {
            if (cur == head)
            {
                head = cur->next;
                free(cur);
                cur = head;
            }
            else
            {
                prev->next = cur->next;
                free(cur);
                cur = prev->next;
            }
        }
        else
        {
            prev = cur;
            cur = cur->next;
        }
    }
    return head;
}
int main()
{
    Node* n1 = (Node*)malloc(sizeof(Node));
    n1->val = 6;
    Node* n2 = (Node*)malloc(sizeof(Node));
    n2->val = 1;
    Node* n3 = (Node*)malloc(sizeof(Node));
    n3->val = 2;
    Node* n4 = (Node*)malloc(sizeof(Node));
    n4->val = 6;
    Node* n5 = (Node*)malloc(sizeof(Node));
    n5->val = 3;
    Node* n6 = (Node*)malloc(sizeof(Node));
    n6->val = 4;
    Node* n7 = (Node*)malloc(sizeof(Node));
    n7->val = 6; 
    
    n1->next = n2;
    n2->next = n3; 
    n3->next = n4; 
    n4->next = n5;
    n5->next = n6;
    n6->next = n7;
    n7->next = NULL;
    Node* head = removeElements(n1, 6);
	return 0;
}